export default class Requset {
  #baseUrl = "";
  constructor(baseUrl) {
    this.#baseUrl = baseUrl;
  }

  request(options = {}) {
    const { url, header: optHeader, ...otherOpts } = options;
    const header = Object.assign(
      {
        "content-type": "application/json",
      },
      optHeader
    );

    const tmpUrl = `${this.#baseUrl}${url}`;

    return new Promise((resolve, reject) => {
      wx.request({
        header,
        ...otherOpts,
        url: tmpUrl,
        success(res) {
          if (res.data.code != "200") {
            let { message } = res.data;
            reject(message);
          } else {
            resolve(res.data);
          }
        },
        fail(err) {
          reject(err);
        },
      });
    });
  }

  get(options) {
    return this.request(options);
  }
  post(options) {
    options.method = "POST";
    return this.request(options);
  }
}
